﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using AjaxControlToolkit;

namespace BusinessDecision.JobAdministrator.Web
{
    public class TabPanelCollection : AjaxControlToolkit.TabPanelCollection
    {
        private Control _owner;
        private int _version;
        private int _size;
        private Control[] _controls;
        private string _readOnlyErrorMsg;
        private int _defaultCapacity;
        private int _growthFactor;


        public TabPanelCollection(Control owner) : base(owner)
        {
            this._defaultCapacity = 5;
            this._growthFactor = 4;
            if (owner == null)
            {
                throw new ArgumentNullException("owner");
            }
            this._owner = owner;
        }

        public override void Add(Control child)
        {
            if (!(child is TabPanel))
            {
                //throw new ArgumentException("TabPanelCollection can only contain TabPanel controls.");
            }
            if (child == null)
            {
                throw new ArgumentNullException("child");
            }
            if (this._readOnlyErrorMsg != null)
            {
                throw new HttpException("read only");
            }
            if (this._controls == null)
            {
                this._controls = new Control[this._defaultCapacity];
            }
            else if (this._size >= this._controls.Length)
            {
                Control[] destinationArray = new Control[this._controls.Length*this._growthFactor];
                Array.Copy(this._controls, destinationArray, this._controls.Length);
                this._controls = destinationArray;
            }
            int index = this._size;
            this._controls[index] = child;
            this._size++;
            this._version++;
        }

        public override void AddAt(int index, Control child)
        {
            if (index == -1)
            {
                this.Add(child);
            }
            else
            {
                if (child == null)
                {
                    throw new ArgumentNullException("child");
                }
                if ((index < 0) || (index > this._size))
                {
                    throw new ArgumentOutOfRangeException("index");
                }
                if (this._readOnlyErrorMsg != null)
                {
                    throw new HttpException("read only");
                }
                if (this._controls == null)
                {
                    this._controls = new Control[this._defaultCapacity];
                }
                else if (this._size >= this._controls.Length)
                {
                    Control[] destinationArray = new Control[this._controls.Length * this._growthFactor];
                    Array.Copy(this._controls, destinationArray, index);
                    destinationArray[index] = child;
                    Array.Copy(this._controls, index, destinationArray, index + 1, this._size - index);
                    this._controls = destinationArray;
                }
                else if (index < this._size)
                {
                    Array.Copy(this._controls, index, this._controls, index + 1, this._size - index);
                }
                this._controls[index] = child;
                this._size++;
                this._version++;
            }
        }
    }
}
